package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.VerificationCodeBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.StringUtil;
import com.dinadi.yidi.utils.ToastUtil;

/**
 * author: JiaYu.He
 * Date: 2017/3/28  注册页面
 * Time: 13:58
 */

public class RegisterActivity extends BaseActivity {
    private View view;
    private ImageView canle_register, clear_phone, btn_login;
    private EditText edit_phone, verification_code;
    private TextView text_send_verification_code, goto_html;
    private String phone, code;
    // 倒计时
    private TimeCount timeCount;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.activity_register, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        canle_register = (ImageView) view.findViewById(R.id.canle_register);
        clear_phone = (ImageView) view.findViewById(R.id.clear_phone);
        btn_login = (ImageView) view.findViewById(R.id.btn_login);
        edit_phone = (EditText) view.findViewById(R.id.edit_phone);
        verification_code = (EditText) view.findViewById(R.id.verification_code);
        text_send_verification_code = (TextView) view.findViewById(R.id.text_send_verification_code);
        goto_html = (TextView) view.findViewById(R.id.goto_html);
        edit_phone.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0)
                    clear_phone.setVisibility(View.GONE);
                else
                    clear_phone.setVisibility(View.VISIBLE);
            }
        });

    }

    @Override
    protected void initListener() {
        registerClickListener(canle_register, clear_phone, text_send_verification_code, btn_login, goto_html);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.canle_register:
                RegisterActivity.this.finish();
                break;
            case R.id.clear_phone:
                edit_phone.setText("");
                break;
            case R.id.text_send_verification_code:
                sendCode();
                break;
            case R.id.btn_login:
                goToRegister();
                break;
            case R.id.goto_html:
                Intent intent = new Intent(RegisterActivity.this, UserUnderstandingActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }

    }

    //发送验证码
    private void sendCode() {
        phone = edit_phone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            toast(R.string.toast_phone);
            return;
        } else if (!StringUtil.isPhoneNumber(phone)) {
            toast(R.string.edit_need_phone_no);
            return;
        } else {
            //发送请求消息
            VerificationCodeBean bean = new VerificationCodeBean();
            bean.setPhone(phone);
            bean.setType("1");
            CommonInterface verificationCodeInterface = new CommonInterface(RegisterActivity.this, httpListener);
            verificationCodeInterface.getInterface("10003", JSON.toJSONString(bean));
        }
    }


    private void getLogin() {
        /*下一步*/
        Intent intent = new Intent(RegisterActivity.this, NextSettingActivity.class);
        intent.putExtra("nextType", "0");
        intent.putExtra("phone", phone);
        intent.putExtra("code", code);
        startActivity(intent);
    }

    //请求注册
    private void goToRegister() {
        phone = edit_phone.getText().toString();
        code = verification_code.getText().toString();

        if (TextUtils.isEmpty(phone)) {
            toast(R.string.toast_phone);
            return;
        } else if (!StringUtil.isPhoneNumber(phone)) {
            toast(R.string.edit_need_phone_no);
            return;
        } else if (TextUtils.isEmpty(code)) {
            toast(R.string.toast_identifying_code);
            return;
        }
        //开始下一步   判断验证码是否正确

        //发送请求消息
        VerificationCodeBean bean = new VerificationCodeBean();
        bean.setPhone(phone);
        bean.setValidcode(code);
        CommonInterface verificationCodeInterface = new CommonInterface(RegisterActivity.this, isCorrectHttpListener);
        verificationCodeInterface.getInterface("10006", JSON.toJSONString(bean));


    }

    HttpListener httpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                timeCount = new TimeCount(60000, 1000);// 构造CountDownTimer对象
                timeCount.start();// 开始计时
                toast("发送验证码成功");
            } else {
                ToastUtil.showToast("发送失败");
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            ToastUtil.showToast("发送失败");
        }
    };
    HttpListener isCorrectHttpListener = new HttpListener() {
        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                getLogin();
            } else {
                ToastUtil.showToast(errMsg);
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            ToastUtil.showToast("验证码错误");
        }
    };

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            text_send_verification_code.setText(R.string.Re_verification);
            text_send_verification_code.setTextColor(getResources().getColor(R.color.color_333333));
            text_send_verification_code.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            text_send_verification_code.setClickable(false);
            text_send_verification_code.setText("发送验证码（" + millisUntilFinished / 1000 + ")");
            text_send_verification_code.setTextColor(getResources().getColor(R.color.color_999999));
        }
    }


}
